<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 1. 方案一
      const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
      function chunk1(arr, size) {
        const l = arr.length
        let [index, resIndex] = [0, 0]
        const result = new Array(Math.ceil(l / size))
        while (index < l) {
          result[resIndex++] = arr.slice(index, size + index)
          index += size
        }
        return result
      }
      console.log(chunk1(arr1, 5))

      // 2. 方案二
      function chunk2(arr, size) {
        let [start, end] = [0, 0]
        const result = []
        for (let i = 0; i < Math.ceil(arr.length / size); i++) {
          start = i * size
          end = start + size
          result.push(arr.slice(start, end))
        }
        return result
      }
      console.log(chunk2(arr1, 5))
    </script>
  </body>
</html>
